package com.gdth.http.method;

import android.text.TextUtils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

import java.util.List;

import okhttp3.Dns;

/**
 * Created by xjc on 2019-10-19.
 * use
 */
public class EngDNS implements Dns {
    @Override
    public List<InetAddress> lookup(String s) throws UnknownHostException {

        if (TextUtils.isEmpty(s)) {
            throw new UnknownHostException("hostname == null");
        } else {
            try {
                List<InetAddress> inetAddressList = new ArrayList<>();
                InetAddress[] inetAddresses = InetAddress.getAllByName(s);
                for (InetAddress inetAddress : inetAddresses) {
                    if (inetAddress instanceof Inet4Address) {
                        inetAddressList.add(0, inetAddress);
                    } else {
                        inetAddressList.add(inetAddress);
                    }
                }
                return inetAddressList;
            } catch (NullPointerException ex) {
                UnknownHostException unknownHostException = new UnknownHostException("Broken system behavior");
                unknownHostException.initCause(ex);
                throw unknownHostException;
            }
        }
    }
}

